class GridsController < ApplicationController
  # GET /grids
  # GET /grids.xml
  def index
    @grids = Grid.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @grids }
    end
  end

  # GET /grids/1
  # GET /grids/1.xml

  def show
    @grid = Grid.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @grid }
    end
  end

  # GET /grids/new
  # GET /grids/new.xml

  def new
    @grid = Grid.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @grid }
    end
  end

  # GET /grids/1/edit

  def edit
    @grid = Grid.find(params[:id]).to_hash(:name, :description, :rows, :columns, [:items, :text, :location]).to_json
  end

  # POST /grids
  # POST /grids.xml

  def create
    @grid = Grid.new(params[:grid])

    respond_to do |format|
      if @grid.save
        format.html { redirect_to(edit_grid_url(@grid)) }
        format.xml  { render :xml => @grid, :status => :created, :location => @grid }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @grid.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /grids/1.json

  def update
    @grid = Grid.find(params[:id])
    if @grid.update_attributes(params[:grid])
      head :ok
    else
      head :unprocessable_entity
    end
  end

  # DELETE /grids/1
  # DELETE /grids/1.xml

  def destroy
    @grid = Grid.find(params[:id])
    @grid.destroy

    respond_to do |format|
      format.html { redirect_to(grids_url) }
      format.xml  { head :ok }
    end
  end
end
